package cn.boot.asyncmy.service.impl;

import cn.boot.asyncmy.dao.MemberRepository;
import cn.boot.asyncmy.entity.Member;
import cn.boot.asyncmy.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author liangxin
 * @since 24/08/2019
 */
@Transactional(propagation = Propagation.REQUIRED)
@Service
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberRepository memberRepository;


    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = RuntimeException.class )
    @Override
    public List<Member> saveMemberList(List<Member> members) {
        return memberRepository.saveAll(members);
    }

//    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class )
    @Override
    public Member saveMember(Member m) {
        Member member = null;
        try {
            member= memberRepository.save(m);
//            member= memberRepository.saveAndFlush(m);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return member;
    }

}
